package com.ruoyi.web.controller.module.estate;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.DateUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateBuild;
import com.ruoyi.system.domain.module.estate.EstateVisitRecord;
import com.ruoyi.system.service.module.estate.IEstateVisitRecordService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 支付宝门禁登记记录Controller
 *
 * @author ruoyi
 * @date 2025-09-25
 */
@Validated
@Api(value = "支付宝门禁登记记录控制器", tags = {"支付宝门禁登记记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/estate/visit/record")
public class EstateVisitRecordController extends BaseController {

    private final IEstateVisitRecordService estateVisitRecordService;

    /**
     * 查询支付宝门禁登记记录列表
     */
    @ApiOperation("查询支付宝门禁登记记录列表")
    @PreAuthorize("@ss.hasPermi('module:estateVisitRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateVisitRecord> list(@Validated EstateVisitRecord estateVisitRecord) {
        QueryWrapper<EstateVisitRecord> qw = new QueryWrapper<>();
		SysUser sysUser = getUser();
		//用户类型 0 - 系统管理员  1 - 平台管理员 2 - 物业管理员  3 - 小区管理员
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}

		if(StrUtil.isNotEmpty(estateVisitRecord.getEstateName())){
			qw.like("estate_name",estateVisitRecord.getEstateName());
		}
		if(StrUtil.isNotEmpty(estateVisitRecord.getRealName())){
			qw.and(wrapper->{
				wrapper.like("real_name",estateVisitRecord.getRealName()).or().like("nick_name",estateVisitRecord.getRealName());
			});
		}
		if(StrUtil.isNotEmpty(estateVisitRecord.getMobile())){
			qw.like("mobile",estateVisitRecord.getMobile());
		}

		if(StrUtil.isNotEmpty(estateVisitRecord.getBeginDate())){
			qw.between("visit_time",estateVisitRecord.getBeginDate() + " 00:00:00",estateVisitRecord.getEndDate() + " 23:59:59");
		}
		qw.orderByDesc("id");
        TableDataInfo<EstateVisitRecord> td = PageUtils.buildDataInfo(estateVisitRecordService.page(PageUtils.buildPage(),qw));
        List<EstateVisitRecord> list = td.getRows();
		for (EstateVisitRecord estateVisitRecord1 : list) {
			if(StrUtil.isNotEmpty(estateVisitRecord1.getFormData())){
				String formDataContent = "";
				List<String> formData = ListUtil.getListValueContentByJson(estateVisitRecord1.getFormData());
				for (String s : formData) {
					formDataContent += s + " ";
				}
				estateVisitRecord1.setFormDataList(formData);
				estateVisitRecord1.setFormDataContent(formDataContent);
			}
		}

        return td;
    }

    /**
     * 导出支付宝门禁登记记录列表
     */
    @ApiOperation("导出支付宝门禁登记记录列表")
    @PreAuthorize("@ss.hasPermi('module:estateVisitRecord:export')")
    @Log(title = "支付宝门禁登记记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateVisitRecord> export(@Validated EstateVisitRecord estateVisitRecord) {
		QueryWrapper<EstateVisitRecord> qw = new QueryWrapper<>();
		if(StrUtil.isNotEmpty(estateVisitRecord.getEstateName())){
			qw.like("estate_name",estateVisitRecord.getEstateName());
		}
		if(StrUtil.isNotEmpty(estateVisitRecord.getRealName())){
			qw.and(wrapper->{
				wrapper.like("real_name",estateVisitRecord.getRealName()).or().like("nick_name",estateVisitRecord.getRealName());
			});
		}
		if(StrUtil.isNotEmpty(estateVisitRecord.getMobile())){
			qw.like("mobile",estateVisitRecord.getMobile());
		}

		if(StrUtil.isNotEmpty(estateVisitRecord.getBeginDate())){
			qw.between("visit_time",estateVisitRecord.getBeginDate() + " 00:00:00",estateVisitRecord.getEndDate() + " 23:59:59");
		}

        List<EstateVisitRecord> list = estateVisitRecordService.list(qw);
		for (EstateVisitRecord estateVisitRecord1 : list) {
			Date time = estateVisitRecord1.getVisitTime();
			estateVisitRecord1.setVisitTimeStr(DateUtils.format(time,"yyyy-MM-dd HH:mm:ss"));
		}
        ExcelUtil<EstateVisitRecord> util = new ExcelUtil<>(EstateVisitRecord.class);
        return util.exportExcel(list, "支付宝门禁登记记录");
    }

    /**
     * 获取支付宝门禁登记记录详细信息
     */
    @ApiOperation("获取支付宝门禁登记记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:estateVisitRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateVisitRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateVisitRecord estateVisitRecord = estateVisitRecordService.getById(id);
        return AjaxResult.success(estateVisitRecord);
    }

    /**
     * 新增支付宝门禁登记记录
     */
    @ApiOperation("新增支付宝门禁登记记录")
    @PreAuthorize("@ss.hasPermi('module:estateVisitRecord:add')")
    @Log(title = "支付宝门禁登记记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateVisitRecord estateVisitRecord) {
        return toAjax(estateVisitRecordService.save(estateVisitRecord) ? 1 : 0);
    }

    /**
     * 修改支付宝门禁登记记录
     */
    @ApiOperation("修改支付宝门禁登记记录")
    @PreAuthorize("@ss.hasPermi('module:estateVisitRecord:edit')")
    @Log(title = "支付宝门禁登记记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateVisitRecord estateVisitRecord) {
        return toAjax(estateVisitRecordService.updateById(estateVisitRecord) ? 1 : 0);
    }

    /**
     * 删除支付宝门禁登记记录
     */
    @ApiOperation("删除支付宝门禁登记记录")
    @PreAuthorize("@ss.hasPermi('module:estateVisitRecord:remove')")
    @Log(title = "支付宝门禁登记记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateVisitRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
